package com.nwcl.ysx.service.impl;

import com.nwcl.common.result.Page;
import com.nwcl.common.utils.BeanUtil;
import com.nwcl.ysx.bo.SubscribeBO;
import com.nwcl.ysx.dao.SubscribeDao;
import com.nwcl.ysx.entity.Subscribe;
import com.nwcl.ysx.request.RoomSubPageRequest;
import com.nwcl.ysx.service.SubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liguiqin
 * @date 2023/5/26
 */
@Service
public class SubscribeServiceImpl implements SubscribeService {
    @Autowired
    private SubscribeDao subscribeDao;

    @Override
    public List<SubscribeBO> listByIds(List<Integer> ids, Integer status) {
        List<Subscribe> modelList = subscribeDao.listByIds(ids, status);
        if (CollectionUtils.isEmpty(modelList)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyList(modelList, SubscribeBO.class);
    }

    @Override
    public Integer save(SubscribeBO subscribeBO) {
        Subscribe subscribe = BeanUtil.copy(subscribeBO, Subscribe.class);
        subscribeDao.save(subscribe);
        return subscribe.getId();
    }

    @Override
    public Page<SubscribeBO> page(RoomSubPageRequest param) {
        Page<SubscribeBO> resultPage ;
        List<Subscribe> modelList = subscribeDao.list(param.getUserId());
        if (CollectionUtils.isEmpty(modelList)) {
            resultPage = new Page<>(param.getPageNum(), param.getPageSize(), 0, Collections.emptyList());
            return resultPage;
        }
        List<SubscribeBO> boList = BeanUtil.copyList(modelList, SubscribeBO.class);
        //手工设置分页信息
        resultPage = new Page(param.getPageNum(), param.getPageSize(), boList.size(), boList);
        resultPage = Page.getPageList(resultPage);
        return resultPage;
    }

    @Override
    public Integer countByUserId(Integer userId) {
        return subscribeDao.countByUserId(userId);
    }

    @Override
    public void updateStatusById(Integer id, Integer status) {
        subscribeDao.updateStatusById(id, status);
    }
}
